/*
 * PWM.c
 *
 *  Created on: 6 paź 2014
 *      Author: ksiadmar
 */
#include <avr/io.h>
#include <Consts.h>
#include <DarkDefines.h>


unsigned int stepOffset = MAX / STEPS; // 2000/100 = 20;
void PWM_Init(unsigned int value)
{
	TCCR1A=(1<<WGM11);
	TCCR1B=(1<<WGM13)|(1<<WGM12)|(1<<CS10);
	ICR1  = MAX;
	OCR1A = value;
}

EStatus PWM_manage(bool increase)
{
	if (increase)
	{
		if ((MAX - OCR1A) > stepOffset)
		{
			OCR1A +=stepOffset;
			return ST_INFO;
		}
		else
		{
			OCR1A = MAX;
			return ST_WARNING;
		}
	}
	else
	{
		if (OCR1A > stepOffset)
		{
			OCR1A -=stepOffset;
			return ST_INFO;
		}
		else
		{

			OCR1A = 0;
			return ST_WARNING;
		}
	}
}

bool PWM_changeState(bool state)
{
	if (TRUE == state)
	{
		TCCR1A |= _BV(COM1A1);
	}
	else
	{
		TCCR1A &= ~_BV(COM1A1);
	}
	return state;
}

bool PWM_getState()
{
	return (TCCR1A & _BV(COM1A1));
}

void PWM_setValue(unsigned int value)
{
	OCR1A = value;
}
